package com.hunttown.mes.impl;

import com.hunttown.mes.convert.AnalysisNoticeCatalogConverts;
import com.hunttown.mes.rpc.api.AnalysisNoticeCatalogRpcService;
import com.hunttown.mes.rpc.domain.AnalysisNoticeCatalogDTO;
import com.hunttown.mes.service.AnalysisNoticeCatalogService;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.domain.query.AnalysisNoticeCatalogDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisNoticeCatalogConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:06:28
 */
@Service
public class AnalysisNoticeCatalogRpcServiceImpl implements AnalysisNoticeCatalogRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisNoticeCatalogRpcServiceImpl.class);

    private final AnalysisNoticeCatalogService analysisNoticeCatalogService;

    @Autowired
    public AnalysisNoticeCatalogRpcServiceImpl(AnalysisNoticeCatalogService analysisNoticeCatalogService) {
        this.analysisNoticeCatalogService = analysisNoticeCatalogService;
    }

    @Override
    public AnalysisNoticeCatalogDTO insert(AnalysisNoticeCatalogDTO analysisNoticeCatalogDTO) {
        try {
            return AnalysisNoticeCatalogConverts.convert(analysisNoticeCatalogService.insert(AnalysisNoticeCatalogConverts.convert(analysisNoticeCatalogDTO)));
        } catch (Exception e) {
            logger.error("AnalysisNoticeCatalog create failed.", e);
            throw new RuntimeException("AnalysisNoticeCatalog create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisNoticeCatalogDTO analysisNoticeCatalogDTO) {
        try {
            return analysisNoticeCatalogService.updateInfoById(AnalysisNoticeCatalogConverts.convert(analysisNoticeCatalogDTO));
        } catch (Exception e) {
            logger.error("AnalysisNoticeCatalog update by id failed.", e);
            throw new RuntimeException("AnalysisNoticeCatalog update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisNoticeCatalogDTOQuery analysisNoticeCatalogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisNoticeCatalogDTOQuery.getW_id() != null && analysisNoticeCatalogDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisNoticeCatalogDTOQuery.getW_id());
            }

            if (analysisNoticeCatalogDTOQuery.getIds() != null && !analysisNoticeCatalogDTOQuery.getIds().equals("")) {
                query.put("ids", analysisNoticeCatalogDTOQuery.getIds());
            }

            if (analysisNoticeCatalogDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisNoticeCatalogDTOQuery.getStopFlag());
            }

            if (analysisNoticeCatalogDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisNoticeCatalogDTOQuery.getDeleteFlag());
            }

            return analysisNoticeCatalogService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisNoticeCatalog update by query failed.", e);
            throw new RuntimeException("AnalysisNoticeCatalog update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisNoticeCatalogDTO selectById(Integer id) {
        try {
            return AnalysisNoticeCatalogConverts.convert(analysisNoticeCatalogService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisNoticeCatalog select by id failed.", e);
            throw new RuntimeException("AnalysisNoticeCatalog select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisNoticeCatalogDTO selectObjByQuery(AnalysisNoticeCatalogDTOQuery analysisNoticeCatalogDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return AnalysisNoticeCatalogConverts.convert(analysisNoticeCatalogService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisNoticeCatalog select obj by query failed.", e);
            throw new RuntimeException("AnalysisNoticeCatalog select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisNoticeCatalogDTO> getListForPage(AnalysisNoticeCatalogDTOQuery analysisNoticeCatalogDTOQuery) {
        Query query = Query.page(analysisNoticeCatalogDTOQuery.getPn(), analysisNoticeCatalogDTOQuery.getPageSize());

        if (analysisNoticeCatalogDTOQuery.getIds() != null && !analysisNoticeCatalogDTOQuery.getIds().equals("")) {
            query.put("ids", analysisNoticeCatalogDTOQuery.getIds());
        }

        if (analysisNoticeCatalogDTOQuery.getLikeName() != null && !analysisNoticeCatalogDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisNoticeCatalogDTOQuery.getLikeName());
        }

        if (analysisNoticeCatalogDTOQuery.getOrderBy() != null && analysisNoticeCatalogDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisNoticeCatalogDTOQuery.getOrderBy());
        }

        List<AnalysisNoticeCatalogDTO> list = AnalysisNoticeCatalogConverts.convert(analysisNoticeCatalogService.selectByQuery(query));
        Page<AnalysisNoticeCatalogDTO> page = Page.from(analysisNoticeCatalogDTOQuery.getPn(), analysisNoticeCatalogDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisNoticeCatalogService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisNoticeCatalogService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisNoticeCatalog delete failed.", e);
            throw new RuntimeException("AnalysisNoticeCatalog delete failed, Please order your code.", e);
        }
    }
}